<?php

namespace app\service;

use app\common\PayStatus;
use app\model\VipOrder;
use think\helper\Str;

class OrderService
{
    public static ?OrderService $_instance = null;

    /**
     * @return OrderService|mixed
     */
    public static function instance(): ?OrderService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * @param int    $uid
     * @param string $type
     * @return string
     */
    public function getOrderNo(int $uid = 0, string $type = 'vip'): string
    {
        list($_u_sec, $_sec) = explode(" ", microtime());
        $base_uid = date('ymd') + $uid;
        return $type . $_sec . substr($_u_sec, 2, 3) . sprintf("%08d", $base_uid) . Str::random(8);
    }

    /**
     * @param array $data
     * @return string
     */
    public function setVipOrder(array $data = []): string
    {
        try {
            $order_no         = $this->getOrderNo($data['uid'] ?? 0);
            $data['order_no'] = $order_no;
            $data['create_time']   = $data['date_v'] = myDate();
            $data['status']   = PayStatus::PAY_STATUS_WAITING->value;
            VipOrder::create($data);
            return $order_no;
        } catch (\PDOException $exception) {
            return '';
        }
    }

}